package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import java.awt.SystemColor;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

import Client.BusinessImplements;
import Controler.Add_new_item_controler;
import Controler.Update_Author_Controler;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
  * @author Lucie
 * Class name Update_Author_Panel_first
 * extend  Abstract_Jpanel
 * purpose: choose an author to change his details
 */

public class Update_Author_Panel_first extends Abstract_Jpanel {

	private JComboBox comboBoxauthors;
	private String[] all_authors_name;
	private String[] all_authors_id;
	private ArrayList<String> all_authors;
	
	/**
	 * Create the panel.
	 */
	public Update_Author_Panel_first() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 5));
		comboBoxauthors = new JComboBox();
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(39, 52, 511, 209);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblAuthorName = new JLabel("author name:");
		lblAuthorName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAuthorName.setBounds(45, 45, 107, 23);
		panel.add(lblAuthorName);
		
		comboBoxauthors.setBounds(192, 48, 160, 20);
		panel.add(comboBoxauthors);
		
		JLabel lblChooseAuthorYou = new JLabel("choose author you will like to change hes deatails");
		lblChooseAuthorYou.setForeground(Color.RED);
		lblChooseAuthorYou.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		lblChooseAuthorYou.setBounds(36, 86, 334, 23);
		panel.add(lblChooseAuthorYou);
		
		JLabel lblChooseAuthor = new JLabel("choose author  to update ");
		lblChooseAuthor.setForeground(new Color(25, 25, 112));
		lblChooseAuthor.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblChooseAuthor.setBounds(59, 11, 473, 30);
		add(lblChooseAuthor);
		//go to next screen "Update_Author_Panel_sec" and send to this screen the 
		// author the user chose
		JButton btnGo = new JButton("update");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!get_autor_id().equals(""))
				{
				Update_Author_Panel_sec update_author=new Update_Author_Panel_sec();
		  		on_screen_unload();
		  		BusinessImplements.GetInstance().getMainFrame().setContentPane(update_author);
		  		update_author.setSize(getMaximumSize());
		  		update_author.on_screen_load(get_autor_id(), get_autor_name());
		  		
				}
				else
					error_msg();
					
				
				
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(458, 272, 92, 40);
		add(btnGo);
		//go back to sing in panel
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();
				
				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(57, 272, 92, 40);
		add(btnBack);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	/**
	 * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox 
	 * that this panel use, here we call the controller of this panel
	 * to get all author names and id's and set them at the combobox
	 */
	public void on_screen_load() {
		

		
		Update_Author_Controler update_author_controler=new  Update_Author_Controler();
		all_authors=update_author_controler.get_all_authors();
		
		String[] line;
		all_authors_name = new String[all_authors.size()+1];
		all_authors_id = new String[all_authors.size()+1];
		all_authors_name[0] = "";
		all_authors_id[0] =  "";
		for (int i = 1 ; i <= all_authors.size() ; i++)
		{
			line = all_authors.get(i-1).split(",");
			all_authors_id[i] = line[0];
			all_authors_name[i] = line[1];
			
		}
		
		comboBoxauthors.setModel(new DefaultComboBoxModel(all_authors_name));
		comboBoxauthors.setBounds(132, 32, 121, 20);
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @return author id of the author that was chosen in the combobox
	 */
	public String get_autor_id()
	{
		
		if (!comboBoxauthors.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_authors_name.length;i++)
					if( all_authors_name[i].equals(comboBoxauthors.getSelectedItem()))
						
						return all_authors_id[i];
				   	
			
			}
		
		return "";
	}
	/**
	 * 
	 * @return author name of the author that was chosen in the combobox
	 */
	public String get_autor_name()
	{
		
		if (!comboBoxauthors.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_authors_name.length;i++)
					if( all_authors_name[i].equals(comboBoxauthors.getSelectedItem()))
						
						return all_authors_name[i];
				   	
			
			}
		
		return "";
	}
	/**
	 * this method display showMessageDialog with a failed msg
	 */
	public void error_msg()
	{
	JOptionPane.showMessageDialog(this,"miss author deatails ");
	}
}
